package com.zheng.sch.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.validator.LengthValidator;
import com.zheng.sch.common.constant.SchResult;
import com.zheng.sch.common.constant.SchResultConstant;
import com.zheng.sch.dao.model.*;
import com.zheng.sch.rpc.api.SchGuardianService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 家长管理
 * Created by shuzheng on 2016/11/14.
 */
@Controller
@Api(value = "家长管理", description = "家长管理")
@RequestMapping("/manage/guardian")
public class SchGuardianController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SchGuardianController.class);

	@Autowired
	private SchGuardianService schGuardianService;

	@ApiOperation(value = "家长首页")
	@RequiresPermissions("sch:guardian:read")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/manage/guardian/index.jsp";
	}

	@ApiOperation(value = "家长列表")
	@RequiresPermissions("sch:guardian:read")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order) {
		SchGuardianExample schGuardianExample = new SchGuardianExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			schGuardianExample.setOrderByClause(sort + " " + order);
		}
		List<SchGuardian> rows = schGuardianService.selectByExampleForOffsetPage(schGuardianExample, offset, limit);
		long total = schGuardianService.countByExample(schGuardianExample);
		Map<String, Object> result = new HashMap<>(2);
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}

	@ApiOperation(value = "新增家长")
	@RequiresPermissions("sch:guardian:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		return "/manage/guardian/create.jsp";
	}

	@ApiOperation(value = "新增家长")
	@RequiresPermissions("sch:guardian:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(SchGuardian schGuardian) {
		ComplexResult result = FluentValidator.checkAll()
				.on(schGuardian.getName(), new LengthValidator(1, 200, "姓名"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return new SchResult(SchResultConstant.INVALID_LENGTH, result.getErrors());
		}
		long time = System.currentTimeMillis();
		schGuardian.setCtime(time);
		schGuardian.setOrders(time);
		int count = schGuardianService.insertSelective(schGuardian);
		return new SchResult(SchResultConstant.SUCCESS, count);
	}


	@ApiOperation(value = "修改家长")
	@RequiresPermissions("sch:guardian:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") int id, ModelMap modelMap) {
		SchGuardian schGuardian = schGuardianService.selectByPrimaryKey(id);
		modelMap.put("guardian", schGuardian);
		return "/manage/guardian/update.jsp";
	}

	@ApiOperation(value = "修改家长")
	@RequiresPermissions("sch:guardian:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@PathVariable("id") int id, SchGuardian schGuardian) {
		ComplexResult result = FluentValidator.checkAll()
				.on(schGuardian.getName(), new LengthValidator(1, 200, "姓名"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return new SchResult(SchResultConstant.INVALID_LENGTH, result.getErrors());
		}
		schGuardian.setGuardianId(id);
		int count = schGuardianService.updateByPrimaryKeySelective(schGuardian);
		return new SchResult(SchResultConstant.SUCCESS, count);
	}

	@ApiOperation(value = "删除家长")
	@RequiresPermissions("sch:guardian:delete")
	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		int count = schGuardianService.deleteByPrimaryKeys(ids);
		return new SchResult(SchResultConstant.SUCCESS, count);
	}
}